package com.teamwechat.task;

import cn.hutool.core.date.DateUtil;
import com.teamwechat.mapper.TbUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;

@Component
@Scope("prototype")
public class EmailTask implements Serializable {
    @Autowired
    private JavaMailSender javaMailSender;
    @Value("${emos.email.system}")
    private String mailbox;

    @Autowired
    private TbUserMapper userMapper;

    @Async
    public void sendAsync(SimpleMailMessage mailMessage){

        HashMap<String,String> map=userMapper.searchNameAndDept(55);
        String name = map.get("name");
        String deptName = map.get("dept_name");
        deptName = deptName != null ? deptName : "";
        SimpleMailMessage message = new SimpleMailMessage();
        message.setTo("1276296800@qq.com");
        message.setSubject("员工" + name + "身处高风险疫情地区警告");
        message.setText(deptName + "员工" + name + "，" + DateUtil.format(new Date(), "yyyy年MM月dd日") + "处于" + "，属于新冠疫情高风险地区，请及时与该员工联系，核实情况！");
        //emailTask.sendAsync(message);

        message.setFrom(mailbox);
//        message.setCc(mailbox);
        javaMailSender.send(message);
    }
}